﻿using AccountModule;
using Framework;
using UnityEngine;
using UnityEngine.UI;

namespace View
{
    public class AccountView : ViewProxy
    {
        private ClickTrigger _triggerRecipe;
        private ClickTrigger _triggerHot;
        private ClickTrigger _triggerOrder;

        private Text _textName;
        
        private GameObject _goMales;
        private Image[] _imageMales;
        private GameObject _goFemales;
        private Image[] _imageFemales;
        
        protected override void SetupView()
        {
            LoadRes("Views/Account/AccountView");
        }

        protected override void OnInit()
        {
            _triggerRecipe = ClickTrigger.Get(_transform.Find("Button_Recipe"));
            _triggerRecipe.AddClickListener(OnRecipeClick);
            _triggerHot = ClickTrigger.Get(_transform.Find("Button_Hot"));
            _triggerHot.AddClickListener(OnHotClick);
            _triggerOrder = ClickTrigger.Get(_transform.Find("Button_Order"));
            _triggerOrder.AddClickListener(OnOrderClick);

            _textName = _transform.Find("Scroll View/Viewport/Content/Text_Name").GetComponent<Text>();
            _textName.text = "HI," + AccountManager.instance.GetCurrentAccount().name;
            
            
            _goMales = _transform.Find("Scroll View/Viewport/Content/MaleIcons").gameObject;
            _imageMales = new Image[3];
            for (int i = 0; i < 3; ++i)
            {
                Image image = _goMales.transform.Find("Icon_" + (i + 1)).GetComponent<Image>();
                _imageMales[i] = image;
            }
            
            _goFemales = _transform.Find("Scroll View/Viewport/Content/FemaleIcons").gameObject;
            _imageFemales = new Image[3];
            for (int i = 0; i < 3; ++i)
            {
                Image image = _goFemales.transform.Find("Icon_" + (i + 1)).GetComponent<Image>();
                _imageFemales[i] = image;
            }
            Update();
        }
        
        private void Update()
        {
            Account account = AccountManager.instance.GetCurrentAccount();
            if (account.gender == Gender.Male)
            {
                _goMales.SetActive(true);
                _goFemales.SetActive(false);
                for (var i = 0; i < _imageMales.Length; i++)
                {
                    _imageMales[i].gameObject.SetActive(i == (int)account.aims);
                }
            }
            else
            {
                _goFemales.SetActive(true);
                _goMales.SetActive(false);
                for (var i = 0; i < _imageFemales.Length; i++)
                {
                    _imageFemales[i].gameObject.SetActive(i == (int)account.aims);
                }
            }
        }
        
        private void OnRecipeClick(params object[] args)
        {
            new MyRecipeView().Show();
            Close();
        }
        
        private void OnHotClick(params object[] args)
        {
            new HotRecipeView().Show();
            Close();
        }
        
        private void OnOrderClick(params object[] args)
        {
            new OrderView().Show();
            Close();
        }
    }
}